/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJFrame.java
 *
 * Created on 29-Sep-2010, 15:58:33
 */

package COMIC_BOOK_STORE;

import java.awt.Color;
import java.awt.Frame;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author benson
 */
public class STAFF_LOGIN extends javax.swing.JFrame {

    /** Creates new form NewJFrame */
    public STAFF_LOGIN() {
         super("STAFF LOGIN");

        System.setProperty("Quaqua.tabLayoutPolicy", "wrap");
        try {
            UIManager.setLookAndFeel("ch.randelshofer.quaqua.QuaquaLookAndFeel");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(STAFF_LOGIN.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(STAFF_LOGIN.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(STAFF_LOGIN.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(STAFF_LOGIN.class.getName()).log(Level.SEVERE, null, ex);
        }

        initComponents();
        this.setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        loginPage = new javax.swing.JPanel();
        jLayeredPane1 = new javax.swing.JLayeredPane();
        Login_label = new javax.swing.JLabel();
        Login_TextField = new javax.swing.JTextField();
        Password_label = new javax.swing.JLabel();
        jPasswordField1 = new javax.swing.JPasswordField();
        Enter_Button = new javax.swing.JButton();
        exitButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        Login_label.setFont(new java.awt.Font("Lucida Bright", 1, 14));
        Login_label.setText("Login ID: ");
        Login_label.setBounds(230, 130, 130, 40);
        jLayeredPane1.add(Login_label, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Login_TextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Login_TextFieldActionPerformed(evt);
            }
        });
        Login_TextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                Login_TextFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                Login_TextFieldFocusLost(evt);
            }
        });
        Login_TextField.setBounds(330, 130, 158, 40);
        jLayeredPane1.add(Login_TextField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Password_label.setFont(new java.awt.Font("Lucida Bright", 1, 14));
        Password_label.setText("Password: ");
        Password_label.setBounds(230, 190, 140, 40);
        jLayeredPane1.add(Password_label, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jPasswordField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPasswordField1ActionPerformed(evt);
            }
        });
        jPasswordField1.setBounds(330, 190, 158, 40);
        jLayeredPane1.add(jPasswordField1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Enter_Button.setText("ENTER");
        Enter_Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Enter_ButtonActionPerformed(evt);
            }
        });
        Enter_Button.setBounds(380, 290, 100, 40);
        jLayeredPane1.add(Enter_Button, javax.swing.JLayeredPane.DEFAULT_LAYER);

        exitButton.setText("EXIT");
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });
        exitButton.setBounds(500, 290, 100, 40);
        jLayeredPane1.add(exitButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMAGE_REPOSITORY/LoginBackground copy.jpg"))); // NOI18N
        jLabel1.setText("jLabel1");
        jLabel1.setBounds(0, 0, 640, 370);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout loginPageLayout = new javax.swing.GroupLayout(loginPage);
        loginPage.setLayout(loginPageLayout);
        loginPageLayout.setHorizontalGroup(
            loginPageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 640, Short.MAX_VALUE)
        );
        loginPageLayout.setVerticalGroup(
            loginPageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 640, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(loginPage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 370, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(loginPage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-656)/2, (screenSize.height-408)/2, 656, 408);
    }// </editor-fold>                        

    private void Login_TextFieldActionPerformed(java.awt.event.ActionEvent evt) {                                                
        // TODO add your handling code here:
}                                               

    private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt) {                                                
        // TODO add your handling code here:
}                                               

    private void Enter_ButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             


        String query  = "select staff_code from staff where staff_code = '" + Login_TextField.getText().trim() + "'";
        String query2 = "select staff_password from staff where staff_code = '" + Login_TextField.getText().trim() + "'";
        String query3 = "select staff_class, staff_fname from staff where staff_code = '" + Login_TextField.getText().trim() + "'";

//      --------------------------------------------------------------------------------------------------

            try {
                DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
                Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","javaproject","javaproject");

//      --------------------------------------------------------------------------------------------------

                Statement stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery(query);
                Statement stmt2 = con.createStatement();
                ResultSet rs2 = stmt2.executeQuery(query2);
                Statement stmt3 = con.createStatement();
                ResultSet rs3 = stmt3.executeQuery(query3);


//      --------------------------------------------------------------------------------------------------

                String enteredID        = (String)Login_TextField.getText().trim();
                String enteredPassword  = (String)jPasswordField1.getText();
                String staffID          = "";
                String staffPassword    = "";
                String staffClass       = "";
                String staffFname       = "";

                while (rs.next()) {
                staffID = rs.getString("STAFF_CODE");
                }

                while (rs2.next()){
                staffPassword = rs2.getString("STAFF_PASSWORD");
                 }

                while (rs3.next()){
                staffClass = rs3.getString("STAFF_CLASS");
                staffFname = rs3.getString("STAFF_FNAME");
                 }

                staffID         = (String)staffID.trim();
                staffPassword   = (String)staffPassword.trim();
                staffClass      = (String)staffClass.trim();
                staffFname      = (String)staffFname.trim();

                if ((enteredID.equals(staffID))&&(enteredID == null ? "" != null : !enteredID.equals("")))
                {
                    if (enteredPassword.equals(staffPassword))
                    {
                        if (staffClass.equalsIgnoreCase("employee"))
                        {
                          MAIN_MENU.isAdmin = false;
                          System.out.println("staff logged in");
                        }
                        else if (staffClass.equalsIgnoreCase("admin"))
                             {
                              MAIN_MENU.isAdmin = true;
                              System.out.println("admin logged in");
                             }

                    MAIN_MENU.staffOnDuty = staffFname;
//                    BOOK_RENT.staffOnDuty = staffID;
                    new MAIN_MENU().setVisible(true);
                    this.hide();
                    }

                    else
                    {
                        JOptionPane.showMessageDialog(null, "Wrong password!", "ERROR!", JOptionPane.WARNING_MESSAGE);
                    }

                }//end if correct id

                else
                {
                    JOptionPane.showMessageDialog(null, "Invalid user ID!", "ERROR!", JOptionPane.WARNING_MESSAGE);
                }

                System.out.println("OK!");
                
                stmt.close();
                stmt2.close();
                stmt3.close();

            }//end try

            catch (Exception e) {
                System.out.println("error occurred");}

//      --------------------------------------------------------------------------------------------------

    }                                            

    private void Login_TextFieldFocusGained(java.awt.event.FocusEvent evt) {                                            
    String str = Login_TextField.getText();

    if(str.equals("username"))
    {
        Login_TextField.setText(null);
    }
    // TODO add your handling code here:
    }                                           

    private void Login_TextFieldFocusLost(java.awt.event.FocusEvent evt) {                                          
    String str = Login_TextField.getText();

     if(str.equals(""))
    {
        Login_TextField.setText("username");
        Login_TextField.setForeground(Color.LIGHT_GRAY);
    }
    // TODO add your handling code here:
    }                                         

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
    System.exit(0);        // TODO add your handling code here:
    }                                          

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new STAFF_LOGIN().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton Enter_Button;
    private javax.swing.JTextField Login_TextField;
    private javax.swing.JLabel Login_label;
    private javax.swing.JLabel Password_label;
    private javax.swing.JButton exitButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JPanel loginPage;
    // End of variables declaration                   

}
